﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using BrainTechLLC;
using System.ComponentModel;

namespace ImageAppDataModels
{
    public class DirectoriesToExamine : ObservableCollection<DirectoryToExamine>
    {
        public static ICoreDataProvider DefaultDataProvider;
        public ICoreDataProvider _DataProvider;
        public string _Name;

        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name != value)
                {
                    _Name = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Name"));
                }
            }
        }

        public ICoreDataProvider DataProvider
        {
            get
            {
                if (_DataProvider == null)
                {
                    _DataProvider = DefaultDataProvider;
                }
                return _DataProvider;
            }
            set
            {
                if (_DataProvider != value)
                {
                    _DataProvider = value;
                    // HEREHERE?
                }
            }
        }

        public DirectoriesToExamine() { }

        public DirectoriesToExamine(IEnumerable<string> dirs)
        {
            foreach (var dir in dirs)
            {
                Add(new DirectoryToExamine(dir));
            }
        }

        public string[] GetDirectoryNames()
        {
            string[] dirNames = new string[this.Count];

            for (int n = 0; n < Count; n++)
            {
                dirNames[n] = this[n].Directory;
            }

            return dirNames;
        }

        public DirectoriesToExamine(IEnumerable<DirectoryToExamine> dirs)
        {            
            foreach (var dir in dirs)
            {
                Add(dir);
            }
        }

        public void Save(string pathToExaminePath)
        {
            var dirNames = GetDirectoryNames();
            byte[] bytes = dirNames.SerializeToCompressedBinary(true);
            DataProvider.WriteAllBytes(pathToExaminePath, bytes);
        }

        public static string[] Load(string _pathToExaminePath, ICoreDataProvider dataProvider)
        {
            if (dataProvider.Exists(_pathToExaminePath))
            {
                byte[] bytes = dataProvider.ReadAllImageBytes(_pathToExaminePath);
                string[] results = bytes.DeserializeFromCompressedBinary(true) as string[];
                return results;
            }

            return null;
        }
    }

}
